package com.dhcc.sdc.base.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.sdc.base.entity.ContraindicationEntity;
import com.dhcc.sdc.base.service.IContraindicationQcentityService;
import com.dhcc.sdc.base.service.IContraindicationService;
import com.dhcc.sds.base.service.ISdsCommonService;
import com.dhcc.sds.base.service.ISdsQcentityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
@Controller("contraindicationController")
@RequestMapping("${sys.backendPath}/sdc/contraindication")
public class ContraindicationController {

    private String PREFIX = "/backend/sdc/contraindication/";

    @Autowired
    private IContraindicationService contraindicationService;
    @Autowired
    public IContraindicationQcentityService contrainQcentityService;
    @Autowired
    private ISdsCommonService sdsCommonService;
    @Autowired
    ISdsQcentityService qcentityService;
    /**
     * 跳转到禁忌症首页
     */
    @RequestMapping("")
    public String index(Model model) {
        JSONArray entityMap = qcentityService.getUpEntityMap();
        model.addAttribute("entityMap",entityMap);
        JSONArray contraindicationMap = contraindicationService.getMap();
        model.addAttribute("contraindicationMap",contraindicationMap);
        return PREFIX + "contraindication.html";
    }

    /**
     * 跳转到新增病种相关禁忌症
     */
    @RequestMapping("/to_add")
    public String toAdd(Model model) {
        JSONArray entityMap = qcentityService.getUpEntityMap();
        model.addAttribute("entityMap",entityMap);
        return PREFIX + "contraindication_add.html";
    }

    /**
     * 跳转到修改病种相关禁忌症
     */
    @RequestMapping("/to_update/{contraindicationId}")
    public String toUpdate(@PathVariable Long contraindicationId, Model model) {
//        JSONArray entityMap = sdsCommonService.getEntityMap();
//        model.addAttribute("entityMap",entityMap);
        ContraindicationEntity contraindication = this.contraindicationService.selectById(contraindicationId);
//        List<Long> entityIds = contrainQcentityService.getEntityId(contraindicationId);
//
//        contraindication.setEntityIdList(entityIds);

        model.addAttribute("contraindication",contraindication);
        return PREFIX + "contraindication_edit.html";
    }




    /**
     * 获取病种相关禁忌症列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/listPage")
    @ResponseBody
    public Object listPage(@RequestParam Map<String, Object> params) {
        Page<ContraindicationEntity> page = new PageFactory<ContraindicationEntity>().defaultPage();
        List<ContraindicationEntity> result = this.contraindicationService.getList(page,params);
        page.setRecords((List<ContraindicationEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }


    @RequestMapping("/query")
    @ResponseBody
    public Object query(@RequestParam Map<String, Object> params){
        Page<ContraindicationEntity> page = new PageFactory<ContraindicationEntity>().defaultPage();
        List<ContraindicationEntity> result = this.contraindicationService.query(page,params);
        page.setRecords((List<ContraindicationEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }
    /**
     * 新增病种相关禁忌症
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增病种相关禁忌症")
    public Object add(@RequestBody Map<String,Object> params) {
        contraindicationService.add(params);
        return R.ok();
    }

    /**
     * 删除病种相关禁忌症
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除病种相关禁忌症")
    public Object delete(@RequestParam Long contraindicationId) {
        contraindicationService.delete(contraindicationId);
        return R.ok();
    }

    /**
     * 保存病种相关禁忌症
     */
    /**
     * 保存病种相关禁忌症
     */
    @PostMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改病种相关禁忌症")
    public Object update(@RequestBody Map<String,Object> params) {
        contraindicationService.update(params);
        return R.ok();
    }


    @RequestMapping("to_import")
    public String toImport() {
        return PREFIX + "import.html";
    }
    /**
     * 导入
     */
    @RequestMapping(method = RequestMethod.POST, path = "/import")
    @ResponseBody
    public R importItem(@RequestPart("file") MultipartFile file) {
        ExcelReader reader;
        try {
            reader = ExcelUtil.getReader(file.getInputStream(),0);
            List<List<Object>> contraindicationList = reader.read();
            this.contraindicationService.importItem(contraindicationList);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return R.error(e.getMessage());
        }
        return R.ok();
    }
}
